package com.hy;


import java.io.FileInputStream;
import java.io.IOException;
import java.sql.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Properties;

public class JdbcTriangle {
    public String DriverDemo;
    public String userDemo;
    public String urlDemo;
    public String passwordDemo;
    public String table;

    public static void main(String[] args) throws SQLException, IOException, ClassNotFoundException {
        new JdbcTriangle().getCon("src/com/hy/lib/mys.txt");
    }
    public  Object[][] getCon(String furl) throws IOException, ClassNotFoundException, SQLException {
        Properties properties = new Properties();
        properties.load(new FileInputStream(furl));
        DriverDemo = properties.getProperty("DriverDemo");
        urlDemo = properties.getProperty("urlDemo");
        passwordDemo = properties.getProperty("passwordDemo");
        userDemo = properties.getProperty("userDemo");
        table = properties.getProperty("table");
        Class.forName(DriverDemo); //加载数据库驱动 这个路径下的静态代码会被执行

        String url = urlDemo;//设置数据库的地址 设置编码  支持汉字
        String user = userDemo;//用户名
        String password = passwordDemo;//用户密码
        Connection conn = DriverManager.getConnection(url, user, password);//使用mysql驱动当中的连接数据库的API

        //执行SQL语句的步骤
        //第一步
        Statement stat = conn.createStatement();
        //第二步
        String command = "select * from " +table;
        //第三步
        ResultSet result = stat.executeQuery(command);
        ResultSetMetaData metaData = result.getMetaData();
        int columnCount = metaData.getColumnCount();
        List<Object[]> objects = new ArrayList<>();

        //按行读取查询结果当中的数据
        while (result.next()) {
            Object[] fileds = new Object[columnCount];
            for(int i=0;i<columnCount;i++){
                fileds[i]=result.getString(i+1);
            }
            //使用访问器方法获取信息
            //System.out.println(result.getString(1) + " " + result.getString(2) + " " + result.getString(3));
            System.out.println(Arrays.toString(fileds));

            objects.add(fileds);
        }
        Object[][] objects1 = new Object[objects.size()][];

        for (int i = 0; i < objects.size(); i++) {

            objects1[i] = objects.get(i);
        }

        return objects1;
    }
}




